package io.esirong.widget.view;

import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;

import java.lang.ref.WeakReference;

/**
 * 倒计时时间处理
 * Created by esirong on 2016/9/10.
 */
public class CountDownTimer {
    private static final int TICK_WHAT = 2;
    private final long mCountdownInterval;
    private long mMillisRemind;
    private boolean mStarted;
    private boolean mRunning;
    private long mStopTimeInFuture;
    private Handler mHandler = new TickingHandler(this);
    private OnTickListener mOnTickListener;

    /**
     * @param millisInFuture    总倒计时时间
     * @param countDownInterval 倒计时间隔时间
     */
    public CountDownTimer(long millisInFuture, long countDownInterval) {
        mMillisRemind = millisInFuture;
        mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisRemind;
        mCountdownInterval = countDownInterval;

    }

    public void start(long millisInFuture) {
        mMillisRemind = millisInFuture;
        mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisRemind;
        start();
    }

    public void start() {
        mStarted = true;
        updateRunning();
    }

    public void stop() {
        mStarted = false;
        updateRunning();
    }

    private void updateRunning() {
        boolean running = mStarted;
        if (running != mRunning) {
            if (running) {
                dispatchChronometerTick();
                mHandler.sendMessageDelayed(Message.obtain(mHandler, TICK_WHAT), mCountdownInterval);
            } else {
                mHandler.removeMessages(TICK_WHAT);
            }
            mRunning = running;
        }
    }

    void dispatchChronometerTick() {
        final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
        mMillisRemind = millisLeft;
        if (millisLeft <= 0) {
            mMillisRemind = 0;
            stop();
        }
        if (mOnTickListener != null) {
            mOnTickListener.onTick(this);
        }
        if (mOnTickListener != null) {
            if (millisLeft <= 0) {
                mOnTickListener.onComplete(this);
            }

        }
    }

    public long getMillisRemind() {
        return mMillisRemind;
    }

    public void setOnTickListener(OnTickListener listener) {
        this.mOnTickListener = listener;
    }

    /**
     * A callback that notifies when the chronometer has incremented on its own.
     */
    public interface OnTickListener {

        /**
         * Notification that the chronometer has changed.
         */
        void onTick(CountDownTimer countDownTimer);

        void onComplete(CountDownTimer countDownTimer);
    }

    //时间滴嗒消息处理
    static class TickingHandler extends Handler {
        private final WeakReference<CountDownTimer> mService;

        TickingHandler(CountDownTimer service) {
            mService = new WeakReference<>(service);
        }

        @Override
        public void handleMessage(Message msg) {
            CountDownTimer service = mService.get();
            if (service != null) {
                if (service.mRunning) {
                    service.dispatchChronometerTick();
                    sendMessageDelayed(Message.obtain(this, TICK_WHAT), service.mCountdownInterval);
                }
            }
        }
    }

}





